package com.gw.vip.server.service.tool_service;

import com.gw.vip.common.constants.VipConstants;
import com.gw.vip.common.enums.StatusEnums;
import com.gw.vip.common.util.Snowflake;
import com.gw.vip.server.dto.PanOpenDto;
import com.gw.vip.server.dto.PanRefundDto;
import com.gw.vip.server.dto.PanSaleDto;
import com.gw.vip.server.enums.CardStatusEnums;
import com.gw.vip.server.enums.OrderTypeEnums;
import com.gw.vip.server.po.*;
import com.gw.vip.server.service.dao_service.SysCapitalTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;

/**
 * @author GW 光武
 * @description
 * @since 2023-03-25
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PanToolService {

    private final Snowflake snowflake;
    private final SysCapitalTypeService sysCapitalTypeService;

    /**
     * 帐户排序
     * @param accountList
     * @return
     */
    public void sortAcc(List<Account> accountList) {
        List<SysCapitalType> sysCapitalTypeList = sysCapitalTypeService.list();
        accountList.sort(Comparator.comparing(userAccount -> sysCapitalTypeService.getPayOrder(sysCapitalTypeList, userAccount.getCapitalId())));
    }

    public BsOrder init(PanOpenDto dto) {
        BsOrder order = new BsOrder();
        order.setId(snowflake.nextStringId());
        order.setClientOrder(dto.getClientOrder());
        order.setShopId(dto.getShopId());
        order.setUserId(dto.getUserId());
        order.setPan(dto.getPan());
        order.setOrderType(OrderTypeEnums.OPEN.getKey());
        order.setTxnAmt(dto.getPayAmt().add(dto.getRebateAmt()));
        order.setPayAmt(dto.getPayAmt());
        order.setRebateAmt(dto.getRebateAmt());
        order.setBalAmt(BigDecimal.ZERO);
        order.setStatus(StatusEnums.TRANSACTION.getCode());
        order.setRefundAmt(BigDecimal.ZERO);
        order.setCompleteTime(LocalDateTime.now());
        order.setCrtTime(order.getCompleteTime());
        order.setCrtName(VipConstants.SYS);
        order.setUpdTime(order.getCrtTime());
        order.setUpdName(order.getCrtName());
        return order;
    }

    public CardInfo initCardInfo(CardMake cardMake) {
        CardInfo cardInfo = new CardInfo();
        BeanUtils.copyProperties(cardMake, cardInfo);
        cardInfo.setStatus(CardStatusEnums.SUCCESS.getKey());
        cardInfo.setCrtTime(LocalDateTime.now());
        cardInfo.setUpdTime(cardInfo.getCrtTime());
        return cardInfo;
    }

    public BsOrder initTrans(PanSaleDto dto, CardInfo cardInfo) {
        BsOrder order = new BsOrder();
        order.setId(snowflake.nextStringId());
        order.setClientOrder(dto.getClientOrder());
        order.setShopId(dto.getShopId());
        order.setUserId(cardInfo.getUserId());
        order.setPan(cardInfo.getId());
        order.setOrderType(OrderTypeEnums.SALE.getKey());
        order.setTxnAmt(dto.getTxnAmt());
        order.setPayAmt(BigDecimal.ZERO);
        order.setRebateAmt(BigDecimal.ZERO);
        order.setBalAmt(BigDecimal.ZERO);
        order.setStatus(StatusEnums.TRANSACTION.getCode());
        order.setRefundAmt(BigDecimal.ZERO);
        order.setCompleteTime(LocalDateTime.now());
        order.setCrtTime(order.getCompleteTime());
        order.setCrtName(VipConstants.SYS);
        order.setUpdTime(order.getCrtTime());
        order.setUpdName(VipConstants.SYS);
        return order;
    }

    public BsOrder initTrans(BsOrder srcBsOrder, PanRefundDto dto) {
        BsOrder order = new BsOrder();
        BeanUtils.copyProperties(srcBsOrder, order);
        order.setId(snowflake.nextStringId());
        order.setClientOrder(dto.getClientOrder());
        order.setShopId(dto.getShopId());
        order.setOrderType(OrderTypeEnums.REFUND.getKey());
        order.setBalAmt(BigDecimal.ZERO);
        order.setStatus(StatusEnums.TRANSACTION.getCode());
        order.setSrcId(srcBsOrder.getId());
        order.setRefundAmt(BigDecimal.ZERO);
        order.setClearDate(null);
        order.setCompleteTime(LocalDateTime.now());
        order.setCrtTime(order.getCompleteTime());
        order.setUpdTime(order.getCrtTime());
        return order;
    }
}
